package com.tcredit.spider;

import java.time.LocalDateTime;
import java.util.concurrent.Future;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.tcredit.spider.web.service.AsyncTestService;

@Component
public class AppRunner implements CommandLineRunner {

	private final Logger logger = LogManager.getLogger(AppRunner.class);

	private final AsyncTestService asyncTestService;

	public AppRunner(AsyncTestService asyncTestService) {
		this.asyncTestService = asyncTestService;
	}

	@Override
	public void run(String... args) throws Exception {
		logger.info("开始时间: " + LocalDateTime.now());

		// 调用异步方法
		Future<String> page1 = asyncTestService.findUser("《《《《《《《《《《《《《《《周扬");
		Future<String> page2 = asyncTestService.findUser("《《《《《《《《《《《《《《《李鑫磊");
		Future<String> page3 = asyncTestService.findUser("《《《《《《《《《《《《《《《杨辉");

		while (!(page1.isDone() && page2.isDone() && page3.isDone())) {
			logger.info("等待3个任务完成。。。");
			Thread.sleep(1000); 
		}

		logger.info("完成时间: " + LocalDateTime.now());
		logger.info("--> " + page1.get());
		logger.info("--> " + page2.get());
		logger.info("--> " + page3.get());
	}

}